<?php

namespace App\Jobs;

use App\Exceptions\Arr;
use App\Models\Appoint;
use App\Models\AppointNum;
use App\Models\Family;
use App\Models\FamilyChild;
use App\Models\HealthPerson;
use App\Models\HealthRecord;
use App\Models\HospitalImportCheckup;
use App\Models\HospitalImportChild;
use App\Models\HospitalUserFamily;
use App\Models\ImportFamily;
use App\Models\Notice;
use App\Models\TempPushRecord;
use App\Wechat\Temp;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class SyncDataCheckup implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $hospitalImportCheckup;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(HospitalImportCheckup $hospitalImportCheckup)
    {
        $this->hospitalImportCheckup=$hospitalImportCheckup;
        $this->onQueue('sync_data_checkup');
        $this->onConnection('redis');
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $hospitalImportCheckup=$this->hospitalImportCheckup;
            $child_id = HospitalImportChild::query()
                //保健手册
                ->where('field3',$hospitalImportCheckup->field2)
                //基础档案
                ->where('field2',$hospitalImportCheckup->field3)
                //->where('hospital_id',$hospitalImportCheckup->hospital_id)
                ->value('id');
            if($child_id){
                $hospitalImportCheckup->child_id = $child_id;
                $hospitalImportCheckup->save();
                $importFamily = ImportFamily::query()
                    ->where('import_id',$child_id)
                    ->where('type',1)
                    ->get();

                $data['weight'] = $hospitalImportCheckup->field16;
                $data['height'] = $hospitalImportCheckup->field17;
                $data['bmi'] = $hospitalImportCheckup->field25;
                $data['head'] = $hospitalImportCheckup->field18;
                $data['obesity'] = $hospitalImportCheckup->field24;
                $data['developmental'] = $hospitalImportCheckup->field83;
                $data['malnutrition'] = $hospitalImportCheckup->field96;
                $data['title'] = $hospitalImportCheckup->field13;
                $data['date'] = $hospitalImportCheckup->field7;
                $data['source'] = 2;
                Log::info($importFamily);

                foreach($importFamily as $v){

                    $data['user_id'] = $v->user_id;
                    $data['family_id'] = $v->family_id;

                    $record = HealthRecord::query()
                        ->where('date',$hospitalImportCheckup->field7)
                        ->where('source',2)
                        ->where('family_id',$v->family_id)
                        ->first();
                    if($record){
                        $record->update($data);
                    }else{
                        HealthRecord::query()->create($data);
                    }
                }

            }

        }catch(\Exception $e){


            $this->fail($e->getMessage());
        }
    }
}
